package com.yuanchu.store.controller;

import com.yuanchu.common.exception.BizCodeEnum;
import com.yuanchu.common.utils.PageResult;
import com.yuanchu.common.utils.R;
import com.yuanchu.store.entity.YoshopUserPointsLogEntity;
import com.yuanchu.store.service.YoshopUserPointsLogService;
import com.yuanchu.store.utils.SecurityUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @version: 1.0
 * @Author: adan
 * @description:
 * @date: 2024-02-19 9:03
 */
@RestController
@RequestMapping("points")
public class UserPointsLogController {

    @Resource
    private YoshopUserPointsLogService userPointsLogService;

    @GetMapping("/log/list")
    public R queryUserPointsLogList(@RequestParam Integer page) {
        Integer userId = SecurityUtil.getUserId();
        PageResult<YoshopUserPointsLogEntity> userPointsLog = userPointsLogService.queryUserPointsLog(userId, page);
        Map<String, Object> map = new HashMap<>();
        map.put("list", userPointsLog);
        return R.ok().put("data", map);
    }

    @PostMapping("/decr")
    public R decrUserPoints(@RequestBody YoshopUserPointsLogEntity point){
        boolean result = false;
        if (!result){
            return R.error(BizCodeEnum.DECR_USER_BALANCE_EXCEPTION.getCode(), BizCodeEnum.DECR_USER_BALANCE_EXCEPTION.getMsg());
        }
        return R.ok();
    }

    @ApiOperation(value = "/points/bonus", tags = "积分赠送")
    @PostMapping("/bonus")
    public R bonusUserPoints(@RequestParam("userId") Integer userId, @RequestParam("orderNo") String orderNo,
                      @RequestParam("pointsBonus") Integer pointsBonus, @RequestParam("storeId") Integer storeId){
        boolean result = userPointsLogService.bonusUserPoints(userId, pointsBonus, orderNo, storeId);
        if (!result){
            return R.error("积分赠送失败");
        }
        return R.ok();
    }
}
